﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JumJob.Models;

namespace JumJob.Controllers
{
    public class JobController : Controller
    {
        jum_jobEntities jumDb;
        //
        // GET: /Job/

        public JobController() 
        {
            
            jumDb = new jum_jobEntities();
        }


        public ActionResult InProcess() 
        {
            List<Job> jobs = (from job in jumDb.jum_job
                              where job.status == JobStatus.EN_PROCESO
                              select job).ToList();
            return View(jobs);
        }

        public ActionResult Index()
        {
          
            return View();
        }

        public ActionResult Posted() 
        {
            ViewData["q"] = Request.QueryString["q"];
            return View();
        }

        [Authorize]
        public ActionResult Post()
        {
            List<Category> categorias = (from category in jumDb.jum_category
                                     select category).ToList();

            ViewData["categorias"] = new SelectList(categorias, "Id", "Name");
            return View();
        }


        [HttpPost]
        public ActionResult Post(JumJob.Models.Job trabajo) 
        {            
            if (ModelState.IsValid) 
            {
                trabajo.created_at = DateTime.Now;
                trabajo.posted_by = User.Identity.Name;
                trabajo.status = JobStatus.CREADO;
                trabajo.affiliate_id = 1;
                jumDb.jum_job.AddObject(trabajo);
                jumDb.SaveChanges();
                return RedirectToAction("Posted", new { q= User.Identity.Name});              
            }
            List<Category> categorias = (from category in jumDb.jum_category
                                         select category).ToList();
            ViewData["categorias"] = new SelectList(categorias, "Id", "Name");
            return View();
        }
     
        public ActionResult Detail(int id) 
        {
            Job rJob=(from job in jumDb.jum_job
                     where job.id==id
                     select job).FirstOrDefault();
            ViewData["job"] = rJob;
            return View(rJob);
        }

    }
}
